<!DOCTYPE html>
<html id="htmlRoot" lang="zh">
<head>
    <meta charset="UTF-8"/>
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
    <link href="../../src/style/common.css" rel="stylesheet"/>
    <title>🎨 高级前端配色助手</title>
    <style>
        .container {
            display: flex;
            flex-direction: column;
            max-width: 1000px;
            margin: auto;
            background: var(--card-bg);
            padding: 25px;
            border-radius: 12px;
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
        }

        h1 {
            text-align: center;
            color: var(--text-color);
        }

        .theme-toggle {
            display: block;
            width: 100%;
            text-align: right;
            margin-bottom: 10px;
        }

        .theme-toggle button {
            background: var(--button-bg);
            color: var(--button-text);
            border: none;
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
        }

        .controls {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .slider-group {
            background: var(--card-bg);
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: var(--text-color);
        }

        input[type="range"] {
            width: 100%;
        }

        .value {
            text-align: right;
            font-size: 14px;
            margin-top: 4px;
        }

        .colors {
            display: flex;
            flex-wrap: wrap;
            gap: 16px;
            margin: 20px 0;
        }

        .color-block {
            flex: 1;
            min-width: 130px;
            border-radius: 10px;
            padding: 16px;
            color: white;
            text-align: center;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);
            transition: transform 0.2s;
        }

        .color-block:hover {
            transform: translateY(-4px);
        }

        .color-block h3 {
            margin: 0 0 6px 0;
            font-size: 15px;
        }

        .color-block p {
            margin: 4px 0;
            font-size: 13px;
            opacity: 0.9;
        }

        .color-block button {
            margin-top: 8px;
            padding: 5px 10px;
            font-size: 12px;
            border: none;
            background: rgba(255, 255, 255, 0.2);
            cursor: pointer;
            border-radius: 4px;
        }

        .color-block button:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .preview {
            margin-top: 30px;
            padding: 20px;
            background: var(--card-bg);
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .preview h2 {
            margin-top: 0;
            border-bottom: 1px solid var(--border-color);
            padding-bottom: 8px;
        }

        .ui-preview {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
            margin-top: 15px;
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 6px;
            font-size: 14px;
            cursor: pointer;
        }

        .card {
            padding: 15px;
            border-radius: 8px;
            min-width: 200px;
        }

        .export {
            margin-top: 20px;
            text-align: center;
        }

        .export textarea {
            width: 100%;
            height: 120px;
            padding: 10px;
            border-radius: 6px;
            border: 1px solid var(--border-color);
            background: var(--card-bg);
            color: var(--text-color);
            font-family: monospace;
        }

        .export button {
            margin-top: 10px;
            padding: 10px 20px;
            background: var(--button-bg);
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>🎨 高级前端配色助手</h1>

    <div class="theme-toggle">
        <button id="themeToggle">🌙 暗色模式</button>
    </div>

    <div class="controls">
        <div class="slider-group">
            <label for="hue">主色调 (Hue)</label>
            <input id="hue" max="360" min="0" type="range" value="210"/>
            <div class="value"><span id="hueValue">210</span>°</div>
        </div>

        <div class="slider-group">
            <label for="saturation">饱和度 (Saturation)</label>
            <input id="saturation" max="100" min="30" type="range" value="70"/>
            <div class="value"><span id="satValue">70</span>%</div>
        </div>

        <div class="slider-group">
            <label for="lightness">亮度 (Lightness)</label>
            <input id="lightness" max="80" min="30" type="range" value="50"/>
            <div class="value"><span id="lightValue">50</span>%</div>
        </div>

        <div class="slider-group">
            <label for="scheme">配色模式</label>
            <select id="scheme">
                <option value="complement">互补色</option>
                <option value="analogous">类比色</option>
                <option value="split">分裂互补</option>
                <option value="triad">三元组</option>
                <option value="tetradic">四分法</option>
            </select>
        </div>
    </div>

    <div id="colorPalette" class="colors"></div>

    <div class="preview">
        <h2>🎨 UI 预览</h2>
        <div class="ui-preview">
            <button id="previewBtn" class="btn">主按钮</button>
            <div id="previewCard" class="card">
                <h3>卡片标题</h3>
                <p>这是一个使用你配色方案的 UI 示例。</p>
            </div>
        </div>
    </div>

    <div class="export">
        <h2>💾 导出 CSS</h2>
        <textarea id="cssOutput" readonly></textarea>
        <button onclick="copyCss()">复制 CSS</button>
    </div>
</div>
<script>
    // 元素
    const hueSlider = document.getElementById('hue');
    const satSlider = document.getElementById('saturation');
    const lightSlider = document.getElementById('lightness');
    const schemeSelect = document.getElementById('scheme');
    const hueValue = document.getElementById('hueValue');
    const satValue = document.getElementById('satValue');
    const lightValue = document.getElementById('lightValue');
    const colorPalette = document.getElementById('colorPalette');
    const cssOutput = document.getElementById('cssOutput');
    const themeToggle = document.getElementById('themeToggle');
    const previewBtn = document.getElementById('previewBtn');
    const previewCard = document.getElementById('previewCard');

    // 色相偏移函数
    function shiftHue(h, deg) {
        return (h + deg) % 360;
    }

    // 获取配色方案
    function getColors(h, s, l) {
        const scheme = schemeSelect.value;
        let hues = [];

        switch (scheme) {
            case 'complement':
                hues = [h, shiftHue(h, 180)];
                break;
            case 'analogous':
                hues = [h, shiftHue(h, 30), shiftHue(h, -30)];
                break;
            case 'split':
                hues = [h, shiftHue(h, 150), shiftHue(h, 210)];
                break;
            case 'triad':
                hues = [h, shiftHue(h, 120), shiftHue(h, 240)];
                break;
            case 'tetradic':
                hues = [h, shiftHue(h, 90), shiftHue(h, 180), shiftHue(h, 270)];
                break;
        }

        return hues.map((hue, index) => ({
            name: getColorName(index, scheme),
            h: hue,
            s: s,
            l: l
        }));
    }

    function getColorName(index, scheme) {
        const names = {
            complement: ['主色', '互补色'],
            analogous: ['主色', '右邻色', '左邻色'],
            split: ['主色', '分裂1', '分裂2'],
            triad: ['主色', '三元1', '三元2'],
            tetradic: ['主色', '四分1', '四分2', '四分3']
        };
        return names[scheme][index] || `颜色${index + 1}`;
    }

    // HSL to HEX
    function hslToHex(h, s, l) {
        l /= 100;
        const a = s * Math.min(l, 1 - l) / 100;
        const f = n => {
            const k = (n + h / 30) % 12;
            const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
            return Math.round(255 * color);
        };
        const r = f(0), g = f(8), b = f(4);
        return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
    }

    // 判断颜色深浅的辅助函数
    function isLightColor(hex) {
        // 去掉 # 符号
        const c = hex.substring(1);
        const rgb = parseInt(c, 16);
        const r = (rgb >> 16) & 0xff;
        const g = (rgb >> 8) & 0xff;
        const b = (rgb >> 0) & 0xff;
        // 计算感知亮度
        const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
        return luma > 128;
    }

    // 生成颜色块
    function updateColors() {
        const h = parseInt(hueSlider.value);
        const s = parseInt(satSlider.value);
        const l = parseInt(lightSlider.value);

        // 在函数开头计算所有动态颜色
        const primaryHex = hslToHex(h, s, l);
        const primaryHoverHex = hslToHex(h, s + 10, l - 10); // 增加饱和度并降低亮度以增强 hover 效果
        const primaryLightHex = hslToHex(h, s - 10, l + 10);
        const infoBgHex = hslToHex((h + 180) % 360, 70, 55);
        const linkHoverHex = hslToHex(h, s, l + 15);

        hueValue.textContent = h;
        satValue.textContent = s;
        lightValue.textContent = l;

        const colors = getColors(h, s, l);
        colorPalette.innerHTML = '';

        // 添加主色调方案的色块
        colors.forEach((color, i) => {
            const {h: ch, s: cs, l: cl, name} = color;
            const hex = hslToHex(ch, cs, cl);
            const hslStr = `hsl(${ch}, ${cs}%, ${cl}%)`;

            const block = document.createElement('div');
            block.className = 'color-block';
            block.style.backgroundColor = hslStr;
            block.style.setProperty('--hover-color', primaryHoverHex); // 动态设置 hover 颜色

            const isLight = cl > 65;
            block.style.color = isLight ? '#333' : 'white';

            block.innerHTML = `
          <h3>${name}</h3>
          <p>${hex}</p>
          <p>${hslStr}</p>
          <button onclick="copyToClipboard('${hex}')">复制 HEX</button>
        `;

            colorPalette.appendChild(block);
        });

        const productionColors = [
            {name: '主色hover', var: "--primary-hover",},
            {name: '主色light', var: "--primary-light",},
            {name: "背景色", var: "--background-color",},
            {name: "文字色", var: "--text-color",},
            {name: "成功", var: "--success-color",},
            {name: "警告", var: "--warning-bg",},
            {name: "危险", var: "--danger-bg",},
            {name: "信息", var: "--info-bg",},
            {name: "卡片背景", var: "--card-bg",},
        ];

        productionColors.forEach(item => {
            const block = document.createElement('div');
            block.className = 'color-block';
            const color = getComputedStyle(document.getElementById('htmlRoot')).getPropertyValue(item.var);
            block.style.backgroundColor = color;
            const isLight = isLightColor(color);
            block.style.color = isLight ? '#333' : 'white';
            block.innerHTML = `
        <h3>${item.name}</h3>
        <p>${item.var}</p>
        <p>${color}</p>
        <button onclick="copyToClipboard('${color}')">复制</button>
      `;
            colorPalette.appendChild(block);
        });

        // 更新预览组件
        previewBtn.style.backgroundColor = primaryHex;
        previewBtn.style.color = l > 60 ? '#333' : 'white';

        previewCard.style.backgroundColor = 'var(--card-bg)';
        previewCard.style.color = 'var(--text-color)';

        // 构建完整的 CSS 变量输出
        cssOutput.value = `
:root {
    /* 🎨 中性色 */
    --color-white: #ffffff;
    --color-black: #000000;

    --background-color: #f8f9fa;
    --secondary-background-color: #E8E8E8;
    --text-color: #212529;
    --color-description: #565656;
    --subtle-text-color: #6c757d;
    --border-color: #686c71;
    --card-bg: #ffffff;
    --box-shadow: rgba(0, 0, 0, 0.08);

    /* 🎯 主题色（由配色工具驱动） */
    --primary-color: ${primaryHex}; /* 主色调 */
    --primary-hover: ${primaryHoverHex};
    --primary-light: ${primaryLightHex};

    /* 🟢 成功色 */
    --success-color: #28a745;
    --success-bg: #e6f9f0;
    --success-border: #b2f2cc;

    /* 🟡 警告色 */
    --warning-color: #333;
    --warning-bg: #ffc107;
    --warning-border: #ffeac2;

    /* 🔴 危险色 */
    --danger-color: white;
    --danger-bg: #dc3545;
    --danger-border: #ffcccc;

    /* 🔵 信息色 */
    --info-color: #ffffff;
    --info-bg: ${infoBgHex};
    --info-border: #c8e9e5;

    /* 🌫️ 灰色系 */
    --gray-100: #f1f1f1;
    --gray-200: #e9ecef;
    --gray-300: #dee2e6;
    --gray-400: #ced4da;
    --gray-500: #adb5bd;
    --gray-600: #868e96;
    --gray-700: #495057;

    /* 🖱️ 按钮 */
    --button-bg: var(--primary-color);
    --button-text: #ffffff;
    --button-hover: var(--primary-hover);
    --button-disabled: #e9ecef;

    /* 🔗 链接样式 */
    --link-color: var(--primary-color);
    --link-hover: ${linkHoverHex};

    /* 🧱 基础样式 */
    --border-radius: 8px;
    --transition-speed: 0.3s;

        `;
    }

    function copyToClipboard(text) {
        navigator.clipboard.writeText(text).then(() => {
            alert(`已复制: ${text}`);
        });
    }

    function copyCss() {
        cssOutput.select();
        document.execCommand('copy');
        alert('✅ 完整设计系统 CSS 已复制到剪贴板！');
    }

    // 暗色模式切换
    themeToggle.addEventListener('click', () => {
        const isDark = document.body.classList.toggle('dark-mode');
        themeToggle.textContent = isDark ? '☀️ 亮色模式' : '🌙 暗色模式';
    });

    // 监听所有输入
    hueSlider.addEventListener('input', updateColors);
    satSlider.addEventListener('input', updateColors);
    lightSlider.addEventListener('input', updateColors);
    schemeSelect.addEventListener('change', updateColors);

    // 初始化
    updateColors();
</script>
</body>
</html>
